<?php
namespace appin\cms\model;

use appin\cms\Model;
use think\model\concern\SoftDelete;

class Adv extends Model
{
    use SoftDelete;

    protected $name = 'cms_adv';
    protected $autoWriteTimestamp = true;
    protected $deleteTime = 'delete_time';
    protected $globalScope = ['UseLang'];
    protected $append = ['src_string'];

    /**
     * @param $query
     * @param $keyword
     */
    public function scopeUseKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->where('title', 'LIKE', '%'.$keyword . '%');
        }
    }

    /**
     * @param $value
     * @param $data
     * @return string
     */
    public function getSrcStringAttr($value, $data)
    {
        return image($data['src']);
    }

    /**
     * 获取广告列表
     * @param string $where
     * @param int $lang
     * @param int $page
     * @param int $limit
     * @param string $order
     * @param bool $cache
     * @return mixed
     */
    public static function lists($where = '', $lang = 0, $page = 1, $limit = 10, $order = 'id desc', $cache = false)
    {
        $lang = !$lang ? session('lang.id') : 1;
        return static::withoutGlobalScope()
            ->where('lang_id', $lang)
            ->UseWhere($where)
            ->page($page, $limit)
            ->order($order)
            ->cache($cache)
            ->select();
    }

}